﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using FreeProperty.Views.Shared;
using FreeProperty.Data;
using System.Data.SqlTypes;

namespace FreeProperty.Views.Main
{
    public partial class SearchProperty : SiteBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                divPageNoFooter.Visible = false;
                SetUI();
            }
            else
            {
                switch (hdnMode.Value.ToUpper())
                {
                    case "SEARCH":
                        hdnCurrentPage.Value = "1";
                        BindPagingData();
                        break;
                    case "SEARCHDATA":
                        BindPagingData();
                        break;
                    case "CHANGESTATE":
                        BindDistrictData();
                        break;
                }
            }

        }

        private void InitPaging()
        {
            if (hdnStatus.Value.Length > 0)
            {
                switch (hdnStatus.Value.ToUpper())
                {

                    case "PAGING":
                        break;
                    case "PREVIOUS":
                        hdnCurrentPage.Value = (Convert.ToInt32(hdnCurrentPage.Value) - 1).ToString();
                        break;
                    case "NEXT":
                        hdnCurrentPage.Value = (Convert.ToInt32(hdnCurrentPage.Value) + 1).ToString();
                        break;
                    case "FIRST":
                        hdnCurrentPage.Value = "1";
                        break;
                    case "LAST":
                        hdnCurrentPage.Value = hdnPageCount.Value;
                        break;
                }
                //BindPagingData();
            }
        }

        private void BindDistrictData()
        {
            District oDistrict = new District();
            FreePropertyUtil.BindDropdown(ddlDistrict, oDistrict.LoadAllNameValue(Convert.ToInt32(ddlState.SelectedValue)), false);

        }

        private PropertyDesc GetSearchDescription()
        {
            PropertyDesc oPropertyDesc = new PropertyDesc();
            oPropertyDesc.DistrictID = Convert.ToInt32(ddlDistrict.SelectedValue);
            oPropertyDesc.PropertyType = (FreePropertyType)Convert.ToInt32(ddlPropertyType.SelectedValue);
            oPropertyDesc.PurposeType = (FreePropertyUsageType)Convert.ToInt32(ddlPurpose.SelectedValue);
            oPropertyDesc.Location = tbLocation.Value.Trim();
            oPropertyDesc.PriceMin = tbMin.Text.Trim().Length > 0 ? Convert.ToInt32(tbMin.Text) : SqlInt32.Null;
            oPropertyDesc.PriceMax = tbMax.Text.Trim().Length > 0 ? Convert.ToInt32(tbMax.Text) : SqlInt32.Null;

            return oPropertyDesc;
        }
        public FreePropertyUsageType GetUseType()
        {
            FreePropertyUsageType oUsage = FreePropertyUsageType.Buy;
            
            return oUsage;
        }

        private void SetUI()
        {
            FreePropertyUtil.BindNameValue(ddlPropertyType, FreePropertyConstants.CACHE_PROPERTY_TYPE);
            FreePropertyUtil.BindNameValue(ddlState, FreePropertyConstants.CACHE_ALL_STATES);
            FreePropertyUtil.BindNameValue(ddlPurpose, FreePropertyConstants.CACHE_USAGE_TYPES);
            ddlState.SelectedIndex = 0;
            BindDistrictData();
        }
        protected void BindPagingData()
        {
            InitPaging();
            //int currentPage = Convert.ToInt32(hdnCurrentPage.Value);
            //int startingIndex = (currentPage - 1) * PagingUtil.PageSize;
            //int itemsToFetch = PagingUtil.PageSize;
            
            PagerData oPagerData = new PagerData { PageSize = PagingUtil.PageSize, PageNumber = Convert.ToInt32(hdnCurrentPage.Value) };
            PropertyDesc oPropertyDesc = GetSearchDescription();
            List<PropertyDesc> lst = oPropertyDesc.LoadAll(oPagerData);
            divPageNoFooter.Visible = lst.Count > 0 ? true : false;
            hdnPageCount.Value = oPagerData.TotalPages.ToString();
            PagingUtil.BindDataToGrid(grdSearchResults, lst, "Views/Main/SearchProperty.aspx", "AD", spanFootServerPaging, hdnCurrentPage, hdnPageCount, lnkFootNext, lnkFootLast, lnkFootFirst, lnkFootPrevious);
        }


        protected void grdSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                PropertyDesc oProvider = (PropertyDesc)e.Row.DataItem;
                GridViewRow row = e.Row;

            }

        }
    }
}